#include "stdafx.h"
#include <LitMetaGroup.h>

namespace Lit
{
	MetaGroup::MetaGroup( MetaObject * parent, const String &name ) : mParent(parent), Named(name){}
	MetaGroup::MetaGroup( const MetaGroup& rk ){ mProps = rk.mProps; mParent = rk.mParent; Named::operator=(rk);}
	MetaGroup& MetaGroup::operator=( const MetaGroup& rk ){ mParent=rk.mParent; mProps = rk.mProps; Named::operator=(rk); return *this; }
	MetaGroup::~MetaGroup(){}
		
	void MetaGroup::setParent( MetaObject*parent ){ mParent=parent; }
	const MetaObject *MetaGroup::parent() const{ return mParent; }
	MetaObject *MetaGroup::parent(){ return mParent; }
	
	void MetaGroup::setProperty( const MetaData& d ){mProps[d.getName()] = d;}
	void MetaGroup::setProperty( const String &name, const String & val )
	{
		if( !hasKey(name) ) mProps[name] = MetaData( this, name, val );
		else mProps[name].setValue( val );
	}

	void MetaGroup::merge( const MetaGroup& rk, bool replace_old )
	{
		if( replace_old )
		{
			for( auto i=rk.begin(); i!=rk.end(); i++ ) setProperty( i->second.getName(), i->second.getValue() );
		}
		else
		{
			for( auto i=rk.begin(); i!=rk.end(); i++ ) 
			{
				if( !hasKey( i->second.getName() ) )
					setProperty( i->second.getName(), i->second.getValue() );
			}
		}
	}
	
	MetaData& MetaGroup::operator[]( const String &name )
	{
		if( mProps.find(name) == mProps.end() )
			mProps[name] = MetaData(this, name);
		return mProps[name];
	}

	MetaGroup::Map::iterator MetaGroup::find( const String &key ){ return mProps.find(key); }
	MetaGroup::Map::const_iterator MetaGroup::find( const String &key ) const{ return mProps.find(key); }
		
	MetaGroup::Map::iterator MetaGroup::begin(){ return mProps.begin(); }
	MetaGroup::Map::iterator MetaGroup::end(){ return mProps.end(); }

	MetaGroup::Map::const_iterator MetaGroup::begin() const{ return mProps.begin(); }
	MetaGroup::Map::const_iterator MetaGroup::end() const{ return mProps.end(); }

	bool MetaGroup::hasKey( const String &name ) const{ return find(name) != end(); }
		
};